package services

import (
	"go-chat/config"
	"go-chat/model"
	"go-chat/utils"
)

type UserService struct {
}

type LoginResult struct {
	Token string `json:"token,omitempty"`
}

// 用户信息视觉类
type UserVO struct {
	ID       uint   `json:"id"`
	UserName string `json:"username"`
	NickName string `json:"nickname"`
	Sex      int    `json:"sex"`
	Email    string `json:"email"`
	Avatar   string `json:"avatar"`
	Status   int    `json:"status"`
}

func FromUserVO(user *model.User) *UserVO {
	return &UserVO{
		ID:       user.ID,
		UserName: user.UserName,
		NickName: user.NickName,
		Sex:      user.Sex,
		Email:    user.Email,
		Avatar:   user.Avatar,
		Status:   user.Status}

}

// 处理用户登录操作
func (services UserService) Login(username, password string) (*LoginResult, int) {
	var user model.User
	var res *LoginResult = &LoginResult{}
	code := 200
	err := config.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		code = 500
		return res, code
	} else if !utils.CheckPasswordHash(password, user.PassWord) {
		code = 500
		return res, code
	}
	//密码正确就生成token
	token, err := utils.GenerateToken(user.ID)
	if err != nil {
		code = 500
		return res, code
	}
	//返回token
	res.Token = token
	return res, code
}

// 处理用户注册
func (services UserService) Register(userInfo *model.User) (string, int) {
	code := 200
	userInfo.Status = 1
	newPassword, err := utils.HashPassword(userInfo.PassWord)
	if err != nil {
		code = 500
		return "", code
	}
	//检测用户名与邮箱是否有效
	isHasUserName := config.DB.Model(&model.User{}).Where("username = ?", userInfo.UserName).First(&model.User{})
	isHasEmail := config.DB.Model(&model.User{}).Where("email = ?", userInfo.Email).First(&model.User{})
	if isHasUserName.Error == nil {
		code = 405
		return "该用户名已存在,请更换一个!", code
	} else if isHasEmail.Error == nil {
		code = 405
		return "该邮箱已被注册!", code
	}
	//插入数据
	res := config.DB.Model(&model.User{}).Create(map[string]interface{}{
		"username":   userInfo.UserName,
		"password":   newPassword,
		"nickname":   userInfo.NickName,
		"email":      userInfo.Email,
		"status":     userInfo.Status,
		"created_at": utils.GetCurrentTime(),
	})
	if res.Error != nil {
		code = 500
	}
	return "注册成功！", code
}

// 获取用户信息
func (services UserService) GetUserInfo(userId uint) (*UserVO, int) {
	var user model.User
	var res *UserVO
	code := 200
	if err := config.DB.Where("id = ?", userId).First(&user).Error; err != nil {
		code = 500
	}
	res = FromUserVO(&user)
	return res, code
}

func (services UserService) UpdateUserInfo(userInfo *model.User, id uint) (*UserVO, int) {
	var res *UserVO
	code := 200
	newPassword, errPassword := utils.HashPassword(userInfo.PassWord)
	err := config.DB.Model(&model.User{}).
		Where("id = ?", id).
		Updates(map[string]interface{}{
			"nickname":   userInfo.NickName,
			"sex":        userInfo.Sex,
			"avatar":     userInfo.Avatar,
			"password":   newPassword,
			"updated_at": utils.GetCurrentTime(),
		}).Error

	if err != nil && errPassword != nil {
		code = 500
	}

	res = FromUserVO(userInfo)
	return res, code
}
